package cn.edu.zjgsu.ito.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.zjgsu.ito.service.UserDemandServiceI;
import cn.edu.zjgsu.ito.service.UserPurchaseServiceI;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.vo.DemandCommentsSaveVo;
import cn.edu.zjgsu.ito.vo.DemandCommentsShowVo;
import cn.edu.zjgsu.ito.vo.DemandIdVo;
import cn.edu.zjgsu.ito.vo.DemandSaveVo;
import cn.edu.zjgsu.ito.vo.DemandSunSaveVo;
import cn.edu.zjgsu.ito.vo.OrderIdVo;
import cn.edu.zjgsu.ito.vo.OrderMessageShowVo;
import cn.edu.zjgsu.ito.vo.OrderNoCourierShowVo;
import cn.edu.zjgsu.ito.vo.OrderStateShowVo;
import cn.edu.zjgsu.ito.vo.PayPricePageShowVo;
import cn.edu.zjgsu.ito.vo.RefundReasonSaveVo;
import cn.edu.zjgsu.ito.vo.UseVouchersSaveVo;
import cn.edu.zjgsu.ito.vo.UserHomeMessageShowVo;

/**
 * 用户求购控制类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/userDemandController")
public class UserDemandController {
	
	@Autowired
	private UserDemandServiceI userDemandService;
	
	@Autowired
	private UserPurchaseServiceI userPurchaseService;
	
	/**
	 * 用户发布订单接口<br/>
	 * @param userReleaseDemandVo
	 * @return
	 */	
	@RequestMapping(value="release",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> release(DemandSaveVo demandSaveVo,HttpSession session){
		
		Map<String, Object> result = new HashMap<String, Object>();
		int userId = (Integer)session.getAttribute("userId");
//			int userId = 1;
		DemandIdVo demandIdVo = userDemandService.release(demandSaveVo,userId);
		
		result.put("msg", demandIdVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	
	/**
	 * 用户发布订单接口<br/>
	 * @param userReleaseDemandVo
	 * @return
	 */	
	@RequestMapping(value="releaseSun",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> releaseSun(DemandSunSaveVo demandSunSaveVo,HttpSession session){
		
		Map<String, Object> result = new HashMap<String, Object>();
		int userId = (Integer)session.getAttribute("userId");
//			int userId = 1;
		DemandIdVo demandIdVo = userDemandService.releaseSun(demandSunSaveVo,userId);
		
		result.put("msg", demandIdVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	
	/**
	 * 订单详情接口
	 * @param demandIdVo
	 * @return
	 */
	@RequestMapping(value="releaseMessage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> releaseMessage(DemandIdVo demandIdVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderMessageShowVo orderMessageShowVo = userDemandService.releaseMessage(demandIdVo);
		
		result.put("msg", orderMessageShowVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	/**
	 * 帮我买首页用户右上角信息
	 * @param session
	 * @return
	 */
	@RequestMapping(value="demandHome",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> demandHome(HttpSession session){
		int userId = -1;
		if(session.getAttribute("userId") != null){
			 userId = Integer.valueOf(session.getAttribute("userId").toString());
		}
		
		Map<String, Object> result = new HashMap<String, Object>();
		List<OrderNoCourierShowVo> orderNoCourierShowVo = userDemandService.wantToBuy(userId);
		UserHomeMessageShowVo userHomeMessageShowVo = userPurchaseService.userHome(userId);
		result.put("msg",orderNoCourierShowVo);
		result.put("user", userHomeMessageShowVo);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 支付页面显示接口
	 * @param orderIdVo
	 * @return
	 */
	@RequestMapping(value="payPricePage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> payPricePage(OrderIdVo orderIdVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		PayPricePageShowVo payPricePageShowVo = userDemandService.payPricePage(orderIdVo);
		
		result.put("msg", payPricePageShowVo);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 支付页面选择代金券接口
	 * @param orderIdVo
	 * @return
	 */
	@RequestMapping(value="payPricePageVoucher",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> payPricePageVoucher(UseVouchersSaveVo useVouchersSaveVo){
		

		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderIdVo orderIdVo = userDemandService.payPricePageVoucher(useVouchersSaveVo);
		
		result.put("msg", orderIdVo);
		result.put("code",Constant.OK);
		return result;
		
	}
	
	
	/**
	 * 用户支付接口
	 * @param orderIdVo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="payPrice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payPrice(OrderIdVo orderIdVo,HttpSession session) {
		System.out.println("=========" +orderIdVo.getOrderId());
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderStateShowVo orderStateShowVo = userDemandService.payPrice(orderIdVo);
		
		result.put("msg", orderStateShowVo);
		result.put("code",Constant.OK);
		
		return result;
	}
	
	/**
	 * 用户确认收货接口
	 * @param orderIdVo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="received",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> received(OrderIdVo orderIdVo,HttpSession session) {
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderStateShowVo orderStateShowVo = userDemandService.received(orderIdVo);
		
		result.put("msg", orderStateShowVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	/**
	 * 用户取消订单
	 * @param courierStateSaveVo
	 * @return
	 */
	@RequestMapping(value="cancelOrder",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> cancelOrder(OrderIdVo orderIdVo){
		
		Map<String,Object> result = new HashMap<String ,Object>();
		
		OrderStateShowVo orderStateShowVo = userDemandService.cancelOrder(orderIdVo);
		result.put("msg", orderStateShowVo);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 用户申请退款
	 * @param orderIdVo
	 * @return
	 */
	@RequestMapping(value="userRefund",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userRefund(RefundReasonSaveVo refundReasonSaveVo){
		
		Map<String,Object> result = new HashMap<String ,Object>();
		
		OrderStateShowVo orderStateShowVo = userDemandService.userRefund(refundReasonSaveVo);
		
		result.put("msg", orderStateShowVo);
		result.put("code",Constant.OK);
		
		return result;
	}
	
	/**
	 * 评价页面
	 * @param OrderIdVo
	 * @return
	 */
	@RequestMapping(value="userCommentsPage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userCommentsPage(OrderIdVo orderIdVo){
		
		Map<String,Object> result = new HashMap<String ,Object>();
		DemandCommentsShowVo demandCommentsShowVo = userDemandService.userCommentsPage(orderIdVo);
		result.put("msg", demandCommentsShowVo);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 求购者评分接口
	 * @param demandCommentsSaveVo
	 * @return
	 */
	@RequestMapping(value="userDemandComments",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userDemandComments(DemandCommentsSaveVo demandCommentsSaveVo){
		
		Map<String,Object> result = new HashMap<String ,Object>();
		
		userDemandService.userDemandComments(demandCommentsSaveVo);
		
		result.put("code", Constant.OK);
		
		return result;
		
	}
	
	
}
